import request from '@/utils/request'

// 文件接口类型定义
export interface FileInfo {
  id: string;
  filename: string;
  originalname: string;
  size: number;
  mimetype: string;
  url: string;
  isPublic: boolean;
  uploader: {
    _id: string;
    name: string;
    email: string;
  };
  relatedLessonPlan?: string;
  downloads: number;
  createdAt: string;
  updatedAt: string;
}

export interface ApiResponse<T> {
  success: boolean;
  message?: string;
  data: T;
  code?: string;
}

export interface FileListParams {
  page?: number;
  limit?: number;
  category?: string;
  isPublic?: boolean;
  uploader?: string;
}

export interface PaginationData<T> {
  files: T[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
  };
}

// 文件API接口
export const fileApi = {
  // 获取文件列表
  getList: (params?: FileListParams): Promise<ApiResponse<PaginationData<FileInfo>>> => {
    return request.get('/files', { params });
  },

  // 文件预览
  preview: (id: string): Promise<ApiResponse<any>> => {
    return request.get(`/files/preview/${id}`);
  },

  // 下载文件
  download: (id: string): Promise<Response> => {
    return fetch(`/api/files/download/${id}`, {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${localStorage.getItem('token')}`
      }
    });
  },

  // 删除文件
  delete: (id: string): Promise<ApiResponse<void>> => {
    return request.delete(`/files/${id}`);
  }
};

export default fileApi;